var walk;

function init() 
{
	// Load the waks from database
	retrieveWalks();
}

// Load the trails in the begining
function retrieveWalks() 
{
  var request = GXmlHttp.create();
  request.open('GET', 'mapHome.php', true);
  request.onreadystatechange = function() 
  {
    if (request.readyState == 4) 
    {
      xmlDoc = request.responseXML;
      if(xmlDoc != null)
      {
        walks = xmlDoc.documentElement.getElementsByTagName("walk");
        
        // Populate the last divs
        var tempHTML = '';  
        for (var i = 0; i < walks.length; i++) 
        {
          tempHTML += addReccoWalk(walks[i].getAttribute("id"), walks[i].getAttribute("title"), walks[i].getAttribute("pntCnt"), walks[i].getAttribute("center"), walks[i].getAttribute("pnts"));
        }
        //tempHTML += tempHTML;
        document.getElementById('walksVisited').innerHTML = tempHTML;
        document.getElementById('walksFavourited').innerHTML = tempHTML;
        document.getElementById('walksCreated').innerHTML = tempHTML;
            
        jQuery("div#scrollWalksVisited").scrollable().reload().end();
        jQuery("div#scrollWalksVisited").scrollable().reload().begin(3000);
        jQuery("div#scrollWalksFavourited").scrollable().reload().end();
        jQuery("div#scrollWalksFavourited").scrollable().reload().begin(3000);
        jQuery("div#scrollWalksCreated").scrollable().reload().end();
        jQuery("div#scrollWalksCreated").scrollable().reload().begin(3000);
        
      }
      else
        alert('Error Loading Data from File! Please try again..');
    } 
  }
  request.send(null);
}

function addReccoWalk(id, title, pntCnt, center, pnt) 
{
  var latlng;
  var pnts = pnt.split(",");
  var idx = 0;
  var latlngs = [];
  
  // construct polyline and static map
  var img = 'http://maps.google.com/maps/api/staticmap?center=' + center + '&zoom=13&size=125x125&maptype=roadmap&format=png&path=color:0x0000ff|weight:5';
  for(i=0; i<pntCnt; i++)
  {
    latlngs[i] = new GLatLng(pnts[idx], pnts[idx+1], true);
    img += '|' + pnts[idx] + ',' + pnts[idx+1];
    idx = idx + 2;
  }  
  img += '&sensor=false&key=ABQIAAAA9k26g8XMhjW26puIcLupsBQX1RsCDEQZuSyNrxjcE1q90HaczRTbVKsY_d5dAheiTvbhhCBgV4eBGw';
  return '<div class="eachWalk"><a href="walks.php?id=' + id + '">' + '<img src="' + img + '" onmouseover="Tip(\'' +title+ '\')" onmouseout="UnTip()"/></a></div>';
}

window.onload = init
window.onunload = GUnload

